package ch.exm.storm.conditions;

import java.util.Set;

import ch.exm.storm.query.relational.RelationalQuery;

public class Not extends AbstractCondition {

	private AbstractCondition condition;
	
	public Not(Condition condition) {
		this.condition = (AbstractCondition)condition;
	}
	
	public boolean matches(Object object) {
		return !condition.matches(object);
	}

	@Override
	public boolean evaluateRelationalCondition(RelationalQuery query, boolean inverse) {
		if (inverse) return condition.evaluateRelationalCondition(query, false);
		
		whereCondition = new StringBuilder();
		if (!condition.evaluateRelationalCondition(query, true)) {
			whereCondition.setLength(0);
			return false;
		}
		if (condition instanceof CompositeCondition) whereCondition.append("(");
		whereCondition.append(condition.getWhereStatement());
		if (condition instanceof CompositeCondition) whereCondition.append(")");
		return true;
	}

	public Set<Class<?>> getQueriedClasses() {
		return condition.getQueriedClasses();
	}
	
	public static Not not(Condition condition) {
		return new Not(condition);
	}
}
